package com.boxing.service;

import com.boxing.beans.Result;
import com.boxing.beans.User;
import com.boxing.beans.UserInfo;
import com.boxing.beans.UserInfoT;
import com.boxing.dao.UserDao;
import com.boxing.utils.Utils;

import javax.servlet.http.HttpServletRequest;

public class UserService {
    UserDao userDao = new UserDao();

    public Result login(User user) {
        boolean result = userDao.login(user);
        if (result){
           return new Result(200, "登录成功");
        }else {
           return new Result(600, "登录失败");
        }
    }

    public Result register(UserInfo userInfo){
        Result result = new Result();
        UserInfo userInfoResult = userDao.unionEmail(userInfo.getEmail());
        if (userInfoResult == null || (userInfoResult.getEmail() != null && userInfoResult.getEmail().isEmpty())){
            // 该邮箱没有被注册
            boolean b = userDao.register(userInfo);
            if (b) {
                result.setCode(200);
                result.setMsg("注册成功");
            }else {
                result.setCode(300);
                result.setMsg("注册失败");
            }
        }else {
            result.setCode(205);
            result.setMsg("该邮箱已被注册，请更换邮箱后重试");
        }
        return result;
    }

    public Result<String> setIcon(UserInfoT userInfoT){
        Result<String> result = new Result<>();
        boolean b = userDao.setIcon(userInfoT);
        if (b){
            result.setCode(200);
            result.setMsg("修改成功");
        }else {
            result.setCode(201);
            result.setMsg("修改失败");
        }
        return result;
    }

    public Result<UserInfoT> getUser(String userEmail) {
        Result<UserInfoT> result = new Result<>();
        UserInfoT userInfo = userDao.getUserInfo(userEmail);
        if (userInfo != null){
            result.setCode(200);
            result.setMsg("");
            result.setData(userInfo);
        }else {
            result.setCode(201);
            result.setMsg("用户不存在");
        }
        return result;
    }
}
